package br.unesp.rc.lista01.ex_03.dao;

import java.util.ArrayList;
import java.util.List;

import br.unesp.rc.lista01.ex_03.beans.Venda;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.ext.Db4oException;

public class VendaDAOImp implements VendaDAO {

	public VendaDAOImp() {
		
	}
	
	public boolean storeVenda(Venda venda) {
    	ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), "database.db4o");
		try {
			db.store(venda);
		} catch (Db4oException e) {
			throw new RuntimeException(e);
		} finally {
			db.close();
		}
		return true;
    }
	
	public List<Venda> listVenda() {
    	ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), "database.db4o");
    	List<Venda> vendas = new ArrayList<Venda>();
        try {
        	ObjectSet<Venda> objects = db.query(Venda.class);
        	for (Venda venda: objects) {
        		vendas.add(venda);
        	}
        } catch (Db4oException e) {
			throw new RuntimeException(e);
		} finally {
			db.close();
		}
        return vendas;
    }
	
}
